package com.lims.utils;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author limaosheng
 * @create 2023-02-08 16:08
 * @desc
 */
public class TimeUtils {

    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * 字符串转成date
     * @author limaosheng
     * @create 16:09 2023/2/8
     * @param str
     */
    public static LocalDateTime strToDate(String str){
        try {
            DateTimeFormatter pattern = DateTimeFormatter.ofPattern(DATE_FORMAT);
            return LocalDateTime.parse(str, pattern);
        } catch (Exception e) {
            return LocalDateTime.now();
        }
    }
    /**
     * 字符串转成时间戳
     * @author limaosheng
     * @create 10:08 2023/3/22
     * @param str
     */
    public static Long strToTime(String str){
        LocalDateTime localDateTime;
        try {
            DateTimeFormatter pattern = DateTimeFormatter.ofPattern(DATE_FORMAT);
            localDateTime = LocalDateTime.parse(str, pattern);
        } catch (Exception e) {
            localDateTime =  LocalDateTime.now();
        }
        return Timestamp.valueOf(localDateTime).getTime();
    }

    public static Long strToLongTime(String str){
        try {
            DateTimeFormatter pattern = DateTimeFormatter.ofPattern(DATE_FORMAT);
            LocalDateTime localDateTime = LocalDateTime.parse(str, pattern);
            return Timestamp.valueOf(localDateTime).getTime();
        } catch (Exception e) {
        }
        return null;
    }
    /**
     * 当前时间转成字符串
     * @author limaosheng
     * @create 10:05 2023/3/22
     * @param
     */
    public static String nowToStr(){
        return DateTimeFormatter.ofPattern(DATE_FORMAT).format(LocalDateTime.now());
    }


}
